package com.itcast.donkey.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.modles.TravelFigureModels;
import com.itcast.donkey.modles.TravelInItemModels;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import static com.itcast.donkey.R.id.rel_bottom;

/**
 * Created by lanouhn on 16/9/13.
 */
public class SecondInItemAdapter extends RecyclerView.Adapter<SecondInItemAdapter.MyHolder> {
    private Context context;
    private TravelInItemModels travelInItemModels;
    private List<TravelInItemModels.TripDaysBean.NodesBean> nodesBeen;
    private List<TravelInItemModels.NotesLikesCommentsBean> notesLikesCommentsBeen;

    public SecondInItemAdapter(Context context, TravelInItemModels inItemModels) {
        this.context = context;
        this.travelInItemModels = inItemModels;

        nodesBeen = new ArrayList<>();
        for (int i = 0; i < inItemModels.getTrip_days().size(); i++) {
            int day = inItemModels.getTrip_days().get(i).getDay();
            List<TravelInItemModels.TripDaysBean.NodesBean> btemp = inItemModels.getTrip_days().get(i).getNodes();
            for (TravelInItemModels.TripDaysBean.NodesBean b : btemp) {
                b.setTripDay(day);
            }
            nodesBeen.addAll(inItemModels.getTrip_days().get(i).getNodes());
        }

        notesLikesCommentsBeen = new ArrayList<>();
        notesLikesCommentsBeen = travelInItemModels.getNotes_likes_comments();
    }


    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_travel_in_item, null);
        MyHolder holder = new MyHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(MyHolder holder, int position) {

        if (position == 0) {
            Picasso.with(context).load(travelInItemModels.getFront_cover_photo_url()).into(holder.frontCoverPhotoUrl);
            Picasso.with(context).load(travelInItemModels.getUser().getImage()).into(holder.image);
            holder.name.setText(travelInItemModels.getName());
            holder.startDate.setText(travelInItemModels.getStart_date());
            holder.travelDay.setText(travelInItemModels.getTrip_days().size() + "天");
            //holder.line.setVisibility(View.VISIBLE);
            holder.travelWhichimage.setText(travelInItemModels.getPhotos_count() + "图");
            holder.relTop.setVisibility(View.VISIBLE);
            holder.day.setVisibility(View.GONE);
            holder.linRecyclerview.setVisibility(View.GONE);
            holder.relInDay.setVisibility(View.GONE);

        } else {
            holder.day.setVisibility(View.GONE);
            holder.relTop.setVisibility(View.GONE);
            holder.relInDay.setVisibility(View.VISIBLE);
            holder.linRecyclerview.setVisibility(View.VISIBLE);

            int p = position - 1;

            if (nodesBeen.get(p).getEntry_name() == null) {
                holder.day.setVisibility(View.VISIBLE);
                holder.relInDay.setVisibility(View.GONE);
                holder.day.setText("DAY" + nodesBeen.get(p).getTripDay());
            } else {
                holder.day.setVisibility(View.GONE);
                holder.relInDay.setVisibility(View.VISIBLE);
                holder.entryName.setText(nodesBeen.get(p).getEntry_name() + "");
                if (nodesBeen.get(position).getMemo().getPrice_amount() != null) {
                    holder.priceAmount.setText("门票:" + nodesBeen.get(position).getMemo().getPrice_amount() + "元");
                }
                if (nodesBeen.get(position).getMemo().getTime() != null) {
                    holder.time.setText("游览:" + nodesBeen.get(position).getMemo().getTime() + "分钟");
                }


            }
            holder.linRecyclerview.removeAllViews();

            for (int z = 0; z < nodesBeen.get(p).getNotes().size(); z++) {
                View view = LayoutInflater.from(context).inflate(R.layout.item_travel_indaycontent_item, null);
                ImageView iv_url = (ImageView) view.findViewById(R.id.iv_url);
                TextView description = (TextView) view.findViewById(R.id.description);
                TextView image_description = (TextView) view.findViewById(R.id.image_description);

                ImageView iv_position = (ImageView) view.findViewById(R.id.iv_position);
                TextView position_entryName = (TextView) view.findViewById(R.id.position_entryName);

                TextView likes_count = (TextView) view.findViewById(R.id.likes_count);
                TextView comments_count = (TextView) view.findViewById(R.id.comments_count);

                int id = nodesBeen.get(p).getNotes().get(z).getId();
                for (int i = 0; i < notesLikesCommentsBeen.size(); i++) {
                    int isLikeId = notesLikesCommentsBeen.get(i).getId();
                    if (id == isLikeId) {
                        comments_count.setText(notesLikesCommentsBeen.get(i).getComments_count() + "");
                        likes_count.setText(notesLikesCommentsBeen.get(i).getLikes_count() + "");
                        break;
                    } else {
                        comments_count.setText("");
                        likes_count.setText("");
                    }
                }

                if (nodesBeen.get(p).getNotes().get(z).getPhoto() == null) {
                    description.setText(nodesBeen.get(p).getNotes().get(z).getDescription() + "");
                    description.setVisibility(View.VISIBLE);
                    image_description.setVisibility(View.GONE);
                    iv_url.setVisibility(View.GONE);
                    likes_count.setVisibility(View.VISIBLE);
                    comments_count.setVisibility(View.VISIBLE);

                } else {
                    if (nodesBeen.get(p).getNotes().get(z).getDescription() == null) {
                        Picasso.with(context).load(nodesBeen.get(p).getNotes().get(z).getPhoto().getUrl()).into(iv_url);

                        iv_url.setVisibility(View.VISIBLE);
                        description.setVisibility(View.GONE);
                        image_description.setVisibility(View.GONE);


                    } else {
                        Picasso.with(context).load(nodesBeen.get(p).getNotes().get(z).getPhoto().getUrl()).into(iv_url);
                        image_description.setText(nodesBeen.get(p).getNotes().get(z).getDescription() + "");
                        iv_url.setVisibility(View.VISIBLE);
                        image_description.setVisibility(View.VISIBLE);
                        description.setVisibility(View.GONE);
                    }
                }
                if (nodesBeen.get(p).getEntry_name() == null) {
                    iv_position.setVisibility(View.GONE);
                    position_entryName.setVisibility(View.GONE);
                } else {
                    iv_position.setVisibility(View.VISIBLE);
                    position_entryName.setText(nodesBeen.get(p).getEntry_name() + "");
                }
                holder.linRecyclerview.addView(view);
            }
        }
    }


    @Override
    public int getItemCount() {
        int size = 0;
        for (int i = 0; i < travelInItemModels.getTrip_days().size(); i++) {
            size += travelInItemModels.getTrip_days().get(i).getNodes().size();
        }
        return size + 1;
    }

    public class MyHolder extends RecyclerView.ViewHolder {

        private ImageView frontCoverPhotoUrl;
        private ImageView image;
        private TextView name;
        private TextView startDate;
        private View line;
        private TextView travelDay;
        private TextView travelWhichimage;
        private TextView day;
        private RelativeLayout relTop;
        private RelativeLayout relInDay;

        private ImageView ivHead;
        private TextView entryName;
        private ImageView score;
        private TextView priceAmount;
        private View view;
        private TextView time;
        private LinearLayout linRecyclerview;

        public MyHolder(View itemView) {
            super(itemView);

            frontCoverPhotoUrl = (ImageView) itemView.findViewById(R.id.front_cover_photo_url);
            image = (ImageView) itemView.findViewById(R.id.image);
            name = (TextView) itemView.findViewById(R.id.name);
            startDate = (TextView) itemView.findViewById(R.id.start_date);
            line = (View) itemView.findViewById(R.id.line);
            travelDay = (TextView) itemView.findViewById(R.id.travel_day);
            travelWhichimage = (TextView) itemView.findViewById(R.id.travel_whichimage);
            day = (TextView) itemView.findViewById(R.id.day);
            relTop = (RelativeLayout) itemView.findViewById(R.id.rel_top);
            relInDay = (RelativeLayout) itemView.findViewById(R.id.rel_inDay);
            ivHead = (ImageView) itemView.findViewById(R.id.iv_head);
            entryName = (TextView) itemView.findViewById(R.id.entry_name);
            score = (ImageView) itemView.findViewById(R.id.score);
            priceAmount = (TextView) itemView.findViewById(R.id.price_amount);
            view = (View) itemView.findViewById(R.id.view);
            time = (TextView) itemView.findViewById(R.id.time);
            linRecyclerview = (LinearLayout) itemView.findViewById(R.id.lin_recyclerview);
        }
    }
}
